package com.suoluo.zenadmin.service.dest;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.suoluo.zenadmin.common.PageUtil;
import com.suoluo.zenadmin.common.StringUtil;
import com.suoluo.zenadmin.common.config.SystemConfigHolder;
import com.suoluo.zenadmin.dao.dest.MobileDAO;
import com.suoluo.zenadmin.vo.classify.FeatureItemVO;
import com.suoluo.zenadmin.vo.classify.HomeStyleVO;
import com.suoluo.zenadmin.vo.dest.DestFeature2VO;
import com.suoluo.zenadmin.vo.dest.DestVO;
import com.suoluo.zenadmin.vo.home.HomeItemPortalVO;
import com.suoluo.zenadmin.vo.news.News2VO;

public class MobileService {

	@Autowired
	private MobileDAO destDAO;
	
//	@Resource(name = "destService")
//	private DestService destService;
//	/**
//	 * 查询导航页特色类别为父类的特色分类信息
//	 * 
//	 * @return 特色分类为父类别的集合
//	 */
//	public List<DestFeatureCategoryPOJO> findHomeParentFeatureCategory(){
//		return destDAO.findHomeParentFeatureCategory(SystemConfigHolder
//				.getDestFeatureHotelCategoryId());
//	}
//	
	public List<HomeItemPortalVO> findAllHomeData(int curPage){
		
			int pageRows = SystemConfigHolder.getMobilePageRows();
			int startPos = PageUtil.calcStartRow(curPage, pageRows);
			List<Map> result = destDAO.findAllHomeData(startPos, pageRows);
			List<HomeItemPortalVO> itemList = new ArrayList<HomeItemPortalVO>();
			
			for (Map<String, Object> row : result){
				HomeItemPortalVO itemVO = new HomeItemPortalVO();
				
				Integer featureCategoryId = (Integer) row.get("t_feature_category_id");
				itemVO.setFeatureCategoryId(featureCategoryId);
				itemVO.setDestId((Integer) row.get("t_dest_id"));
				itemVO.setLinkId((Integer) row.get("t_link_id"));
				String title = (String) row.get("zhname");
				itemVO.setTitle(title);
//				itemVO.setImgPreffix((String) row.get("t_feature_item_img_preffix"));
//				itemVO.setImgName((String) row.get("t_feature_item_img"));
				String source = (String) row.get("source");
				String small = (String) row.get("small");
				String normal = (String) row.get("normal");
				itemVO.setOrigImg(source + normal);
				itemVO.setMiniImg(source + small);
				itemVO.setItemCreateTime((Date) row.get("t_item_create_time"));
				itemVO.setFeatureCategoryName((String) row.get("t_category_name"));
				
				itemVO.setCountryName((String) row.get("t_country_name_zh"));
				
				itemVO.setCountryOrigFlag((String) row.get("t_country_orig_flag"));
				
				itemVO.setDestName((String) row.get("destName"));
				
				itemList.add(itemVO);
			}
			return itemList;
	}
	
	
	/**
	 * 查询首页的下一页的页码
	 * 
	 * @param curPage
	 *            当前也
	 * @return 返回下一页的页码，没有下一页则返回0
	 */
	public int findHomeNextPage(int curPage) {
		int totalCount = destDAO.findHomeItemCount();
		int pageRows = SystemConfigHolder.getMobilePageRows();
		int totalPages = PageUtil.calcPageCount(totalCount, pageRows);
		if (totalPages > curPage) {
			return curPage + 1;
		} else {
			return 0;
		}
	}
	
	/**
	 * 导航页触发某一条特色分类信息，按分页参数展示该类别的特色项
	 * @param startPage 分页的起始页
	 * @param maxPage 分页显示条数
	 * @return
	 */
	public List<FeatureItemVO> findFeatureItemByCategoryId(int featureCategoryId,int curPage)
	{
			int pageRows = SystemConfigHolder.getMobilePageRows();
			int startPos = PageUtil.calcStartRow(curPage, pageRows);
			List<Map> list = destDAO.findFeatureItemByCategoryId(featureCategoryId, startPos, pageRows);
			List<FeatureItemVO> resList = new ArrayList<FeatureItemVO>();
			for (Map hashMap : list) {
				FeatureItemVO featureItemVO = new FeatureItemVO();
				featureItemVO.setFeatureItemId((Integer)hashMap.get("feature_item_id"));
				featureItemVO.setFeatureItemTitle((String)hashMap.get("zhname"));
				String source = (String) hashMap.get("source");
				String small  = (String) hashMap.get("small");
				String normal = (String) hashMap.get("normal");
				featureItemVO.setFeatureItemOrigImg(source + normal);
				featureItemVO.setFeatureItemMiniImg(source + small);
//				featureItemVO.setFeatureCategoryName((String)hashMap.get("t_feature_category_name"));
				featureItemVO.setCountryName((String)hashMap.get("t_country_name_zh"));
				featureItemVO.setCountryOrigFlag((String)hashMap.get("t_country_orig_flag"));
				featureItemVO.setDestName((String)hashMap.get("destName"));
				resList.add(featureItemVO);
			}
			return resList;
	}
	
	public int findNextFeatureItemPage(int featureCategoryId, int curPage) {

		int totalCount = destDAO
				.findFeatureItemCountInCategory(featureCategoryId);
		int pageRows = SystemConfigHolder.getMobilePageRows();
		int totalPages = PageUtil.calcPageCount(totalCount, pageRows);
		if (totalPages > curPage) {
			return curPage + 1;
		} else {
			return 0;
		}
	}
	
	/**
	 * 查询风格偏好首页类别信息集合
	 * 
	 * @return
	 */
	public List<HomeStyleVO> findHomeStyleItems() {
			List<Map> list = destDAO.findHomeStyleItems();
			List<HomeStyleVO> resList = new ArrayList<HomeStyleVO>();
			for (Map hashMap : list) {
				HomeStyleVO item = new HomeStyleVO();
				item.setHomeStyleId((Integer) hashMap.get("t_home_style_id"));
				item.setFeatureCategoryId((Integer) hashMap
						.get("t_feature_category_id"));
				item.setFeatureCategoryName((String) hashMap
						.get("t_feature_category_name"));
				item.setStyleCount((Integer) hashMap.get("t_style_count"));
				item.setStyleOrigImg((String) hashMap.get("t_style_orig_img"));
				item.setFeatureCategoryDesc((String) hashMap
						.get("t_feature_category_desc"));
				resList.add(item);
			}
			return resList;
	}
	
	/**
	 * 查询新闻首页展示所需的数据
	 * @param pages 分页起始页
	 * @return
	 */
	public List<News2VO> findHomeNews(int pageNum)
	{
		
			//分页中每页展示几条数据
			int pageRows = SystemConfigHolder.getMobilePageRows();
			int startPos = PageUtil.calcStartRow(pageNum, pageRows);
			List<News2VO> list = destDAO.findNews(startPos, pageRows);
			//封装到前台显示VO对象
//			List<MobileNewsVO> resList = new ArrayList<MobileNewsVO>();
			for (News2VO news : list) {
				news.setName(StringUtil.buildCompleteName(news.getZhname(), news.getEnname()));
//				MobileNewsVO newsVO = new MobileNewsVO();
//				newsVO.setNewsId((Integer)hashMap.get("t_news_id"));
//				newsVO.setNewsTitle((String)hashMap.get("t_news_title"));
//				newsVO.setNewsOrigImg((String)hashMap.get("t_news_orig_img"));
//				newsVO.setNewsMiniImg((String)hashMap.get("t_news_mini_img"));
//				newsVO.setCountryId((Integer)hashMap.get("t_country_id"));
//				newsVO.setCountryName((String)hashMap.get("t_country_name_zh"));
//				newsVO.setCountryOrigFlag((String)hashMap.get("t_country_orig_flag"));
//				newsVO.setNewsDate((Date) hashMap.get("t_news_date"));
//				resList.add(newsVO);
			}
			return list;
	}
	
	/**
	 * 查询目的地下的特色
	 * 
	 * @param destId
	 * @return
	 */
	public List<DestFeature2VO> findDestFeatures(int destId) {
		
			// 查询出结果
			return destDAO.findDestFeaturesWithoutContentByDestId(destId);
			
//			Map<Object, DestFeaturePOJO> destFeatureMap = new HashMap<Object, DestFeaturePOJO>();
//			for (Map<Object, Object> row : result) {
//				Object featureCategoryId = row.get("t_feature_category_id");
//				DestFeaturePOJO destFeature = destFeatureMap.get(featureCategoryId);
//				if (destFeature == null) {
//					destFeature = new DestFeaturePOJO();
//					destFeature
//							.setFeatureCategoryId((Integer) featureCategoryId);
//					destFeature.setFeatureId((Integer) row.get("t_feature_id"));
//					destFeature.setFeatureCategoryName((String) row
//							.get("t_feature_category_name"));
//					String featureDesc = (String) row.get("t_feature_desc");
//					featureDesc = UBB.clear(featureDesc);
//					if (featureDesc.length() > SystemConfigHolder
//							.getMobileContentLimit()) {
//						destFeature
//								.setFeatureDesc(featureDesc.substring(0,
//										SystemConfigHolder
//												.getMobileContentLimit()));
//					} else {
//						destFeature.setFeatureDesc(featureDesc);
//					}
//
//					destFeature.setFeatureOrigImg((String) row.get("t_feature_orig_img"));
//					destFeature.setFeatureMiniImg((String) row
//							.get("t_feature_mini_img"));
//					// destFeature.setGt150((Long) row.get("gt150"));
//					destFeature.setFeatureCategoryOrder((Integer) row
//							.get("t_feature_category_order"));
//					destFeatureMap.put(featureCategoryId, destFeature);
//				}
//				
//			}
//			List<DestFeaturePOJO> list = new ArrayList<DestFeaturePOJO>(
//					destFeatureMap.values());
//			Collections.sort(list, new DestFeatureComparator());
//			return list;
	}
//	private static class DestFeatureComparator implements Comparator<DestFeaturePOJO> {
//
//		@Override
//		public int compare(DestFeaturePOJO obj1, DestFeaturePOJO obj2) {
//			return obj1.getFeatureCategoryOrder() -  obj2.getFeatureCategoryOrder();
//		}
//	}
	
	
	/**
	 * 根据特色Id查询目的地信息（主要获取目的地名称），手机版调用此方法
	 * 
	 * @param featureId
	 * @return
	 */
	public DestVO findSimplestDestByFeatureId(int featureId) {
		return destDAO.findSimplestDestByFeatureId(featureId);
	}
}
